package com.ruoyi.project.ledger.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.ledger.domain.LedgerBusinessAccount;
import com.ruoyi.project.ledger.domain.LedgerFile;
import com.ruoyi.project.ledger.domain.LedgerFirefightingFacilities;
import com.ruoyi.project.ledger.mapper.LedgerBusinessAccountMapper;
import com.ruoyi.project.ledger.mapper.LedgerFileMapper;
import org.apache.ibatis.jdbc.Null;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerFirefightingEquipmentMapper;
import com.ruoyi.project.ledger.domain.LedgerFirefightingEquipment;
import com.ruoyi.project.ledger.service.ILedgerFirefightingEquipmentService;

/**
 * 消防设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-05-11
 */
@Service
public class LedgerFirefightingEquipmentServiceImpl implements ILedgerFirefightingEquipmentService 
{
    private static final Logger log = LoggerFactory.getLogger(LedgerFirefightingEquipmentServiceImpl.class);

    @Autowired
    private LedgerFirefightingEquipmentMapper ledgerFirefightingEquipmentMapper;
    @Autowired
    private LedgerFileMapper ledgerFileMapper;
    @Autowired
    private LedgerBusinessAccountMapper ledgerBusinessAccountMapper;

    /**
     * 查询消防设备
     * 
     * @param id 消防设备ID
     * @return 消防设备
     */
    @Override
    public LedgerFirefightingEquipment selectLedgerFirefightingEquipmentById(Long id)
    {
        LedgerFirefightingEquipment ledgerFirefightingEquipment = ledgerFirefightingEquipmentMapper.selectLedgerFirefightingEquipmentById(id);
        if (ledgerFirefightingEquipment != null){
            // 获取图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("7");      // 消防设备
            List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList(ledgerFile);
            ledgerFirefightingEquipment.setImgList(imgList);
        }
        return ledgerFirefightingEquipment;
    }

    /**
     * 查询消防设备列表
     * 
     * @param ledgerFirefightingEquipment 消防设备
     * @return 消防设备
     */
    @Override
    public List<LedgerFirefightingEquipment> selectLedgerFirefightingEquipmentList(LedgerFirefightingEquipment ledgerFirefightingEquipment)
    {
        return ledgerFirefightingEquipmentMapper.selectLedgerFirefightingEquipmentList(ledgerFirefightingEquipment);
    }

    /**
     * 新增消防设备
     * 
     * @param ledgerFirefightingEquipment 消防设备
     * @return 结果
     */
    @Override
    public int insertLedgerFirefightingEquipment(LedgerFirefightingEquipment ledgerFirefightingEquipment)
    {
        ledgerFirefightingEquipment.setCreateTime(DateUtils.getNowDate());
        int i = ledgerFirefightingEquipmentMapper.insertLedgerFirefightingEquipment(ledgerFirefightingEquipment);
        // 添加图片
        if(ledgerFirefightingEquipment.getImgs() != null && ledgerFirefightingEquipment.getImgs().length > 0){
            for(String path : ledgerFirefightingEquipment.getImgs()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("7");    // 消防设备
                ledgerFile.setLedgerId(ledgerFirefightingEquipment.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerFirefightingEquipment.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 修改消防设备
     * 
     * @param ledgerFirefightingEquipment 消防设备
     * @return 结果
     */
    @Override
    public int updateLedgerFirefightingEquipment(LedgerFirefightingEquipment ledgerFirefightingEquipment)
    {
        ledgerFirefightingEquipment.setUpdateTime(DateUtils.getNowDate());
        LedgerBusinessAccount account = ledgerBusinessAccountMapper.selectLedgerBusinessAccountByBeforeCompanyName(ledgerFirefightingEquipment.getCompanyName(), ledgerFirefightingEquipment.getStationId());
        if (StringUtils.isNull(account)){
            ledgerFirefightingEquipment.setBusinessId(null);
        }
        int i = ledgerFirefightingEquipmentMapper.updateLedgerFirefightingEquipment(ledgerFirefightingEquipment);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(ledgerFirefightingEquipment.getId());
        ledgerFile.setLedgerType("7");      // 消防设备
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        if(ledgerFirefightingEquipment.getImgs() != null && ledgerFirefightingEquipment.getImgs().length > 0){
            for(String path : ledgerFirefightingEquipment.getImgs()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("7");    // 消防设备
                ledgerFile.setLedgerId(ledgerFirefightingEquipment.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerFirefightingEquipment.getUpdateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 批量删除消防设备
     * 
     * @param ids 需要删除的消防设备ID
     * @return 结果
     */
    @Override
    public int deleteLedgerFirefightingEquipmentByIds(Long[] ids)
    {
        int i = ledgerFirefightingEquipmentMapper.deleteLedgerFirefightingEquipmentByIds(ids);
        for(Long id : ids){
            // 删除台账图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("7");      // 消防设备
            ledgerFileMapper.deleteLedgerImg(ledgerFile);
        }
        return i;
    }

    /**
     * 删除消防设备信息
     * 
     * @param id 消防设备ID
     * @return 结果
     */
    @Override
    public int deleteLedgerFirefightingEquipmentById(Long id)
    {
        int i = ledgerFirefightingEquipmentMapper.deleteLedgerFirefightingEquipmentById(id);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("7");      // 消防设备
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        return i;
    }

    @Override
    public String importEquipment(List<LedgerFirefightingEquipment> equipmentList, Boolean isUpdateSupport, String operName, Long stationId) {
        if (StringUtils.isNull(equipmentList) || equipmentList.size() == 0)
        {
            throw new CustomException("导入消防设备数据不能为空！");
        }
        if (stationId == null){
            throw new CustomException("角色异常！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i=1;
        for (LedgerFirefightingEquipment equipment : equipmentList){
            i++;
            try{
                if (StringUtils.isBlank(equipment.getPosition())){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行、位置不能为空");
                    continue;
                }
                equipment.setStationId(stationId);
                equipment.setCreateId(String.valueOf(stationId));
                LedgerFirefightingEquipment position = ledgerFirefightingEquipmentMapper.selectLedgerFirefightingEquipmentByPosition(equipment.getPosition(), stationId,equipment.getCompanyName());
                if (StringUtils.isNull(position)){
                    LedgerBusinessAccount a = ledgerBusinessAccountMapper.selectLedgerBusinessAccountByBeforeCompanyName(equipment.getCompanyName(),stationId);
                    if (StringUtils.isNotNull(a)){
                        equipment.setBusinessId(a.getId());
                        equipment.setCompanyName(a.getCompanyName());
                    }
                    equipment.setCreateBy(operName);
                    equipment.setCreateTime(DateUtils.getNowDate());
                    this.insertLedgerFirefightingEquipment(equipment);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、消防设备导入成功");
                }
                else if (isUpdateSupport)
                {
                    equipment.setUpdateBy(operName);
                    this.updateLedgerFirefightingEquipment(equipment);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、消防设备更新成功");
                }
                else {
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行、消防设备已存在");
                }
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>第" + i + "行、消防设备导入失败：";
                failureMsg.append(msg + "该行数据信息有误，请仔细检查");
                log.error(msg, e);
            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String checkEquipmentUnique(LedgerFirefightingEquipment ledgerFirefightingEquipment) {
        LedgerFirefightingEquipment equipment = ledgerFirefightingEquipmentMapper.selectLedgerFirefightingEquipmentByPosition(ledgerFirefightingEquipment.getPosition(), ledgerFirefightingEquipment.getStationId(),ledgerFirefightingEquipment.getCompanyName());
        if (equipment != null){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkEquipmentUnique2(LedgerFirefightingEquipment ledgerFirefightingEquipment) {
        Long id = StringUtils.isNull(ledgerFirefightingEquipment.getId()) ? -1L : ledgerFirefightingEquipment.getId();
        LedgerFirefightingEquipment equipment = ledgerFirefightingEquipmentMapper.selectLedgerFirefightingEquipmentByPosition(ledgerFirefightingEquipment.getPosition(), ledgerFirefightingEquipment.getStationId(),ledgerFirefightingEquipment.getCompanyName());
        if (StringUtils.isNotNull(equipment) && equipment.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
